package com.woniuxy.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.woniuxy.entity.Appointment;
import com.woniuxy.entity.CarOrder;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;

@Slf4j
public class Alipay {
    public static String getAliPayOrderStr(CarOrder carOrder) {
        //调用支付宝请求
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
        AlipayTradeWapPayRequest request=new AlipayTradeWapPayRequest();
        //封装请求信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        log.info("订单编号："+carOrder.getOrderNum());
        model.setOutTradeNo(carOrder.getOrderNum());
        log.info("订单名称："+carOrder.getOrderName());
        model.setSubject(carOrder.getOrderName());
        log.info("订单金额："+carOrder.getOrderTotal());
        model.setTotalAmount(carOrder.getOrderTotal()+"");
        model.setProductCode("QUICK_WAP_WAY");
        //设置回传参数
        String passBackParams="orderId="+carOrder.getCarOrderId();
        model.setPassbackParams(passBackParams);
        request.setBizModel(model);
        // 设置同步地址
        request.setReturnUrl(AlipayConfig.return_url);
        // 设置异步地址
        request.setNotifyUrl(AlipayConfig.notify_url);
        //输出页面的表单
        String webForm = "";
        try {
            //调用SDK生成表单
            webForm = client.pageExecute(request).getBody();
        } catch (Exception e) {
            log.info("支付请求发送失败");
            return "fail";
        }
        log.info(webForm);
        return webForm;
    }

    /*public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "app_id=2015052600090779&biz_content={\"timeout_express\":\"30m\",\"product_code\":\"QUICK_MSECURITY_PAY\",\"total_amount\":\"0.01\",\"subject\":\"1\",\"body\":\"我是测试数据\",\"out_trade_no\":\"IQJZSRC1YMQB5HU\"}&charset=utf-8&format=json&method=alipay.trade.app.pay&notify_url=http://domain.merchant.com/payment_notify&sign_type=RSA2&timestamp=2016-08-25 20:26:31&version=1.0&sign=cYmuUnKi5QdBsoZEAbMXVMmRWjsuUj+y48A2DvWAVVBuYkiBj13CFDHu2vZQvmOfkjE0YqCUQE04kqm9Xg3tIX8tPeIGIFtsIyp/M45w1ZsDOiduBbduGfRo1XRsvAyVAv2hCrBLLrDI5Vi7uZZ77Lo5J0PpUUWwyQGt0M4cj8g=";
        String[] list = str.split("&");
        StringBuilder sb = new StringBuilder();
        for (String s : list) {
            int i = s.indexOf("=");
            String substring = s.substring(i + 1);
            sb.append(s.substring(0,i));
           new String(substring.getBytes("UTF-8"), "UTF-8");
        }
    }*/
}
